package controller

import (
	"github.com/gin-gonic/gin"
	"shop-bean/model"
	"shop-bean/request"
	"shop-common/library/serializer"
	"shop-common/library/variables"
	"shop-common/utils/contextArgs"
	"shop-common/utils/uploader"
	"shop-service/business"
)

type FileController struct {
}

// @Tags 附件
// @Summary 普通附件上传(素材管理的资源)
// @Security ApiKeyAuth
// @function: Image
// @accept multipart/form-data
// @Produce  application/json
// @Param file formData file true "普通上传"
// @Success 200 {string} string "{"success":true,"data":{},"msg":""}"
// @Router /file/upload [post]
func (*FileController) Upload(ctx *gin.Context) {
	file, header, err := ctx.Request.FormFile("file")
	defer file.Close()
	if err != nil {
		serializer.Fail(ctx, "获取文件失败")
		return
	}

	validFile, err := uploader.ValidUploadRequest(file, header)
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	userId, _ := contextArgs.GetUserInternalId(ctx)

	upload, err := business.ServiceImpl().FileService.Upload(userId, validFile)
	if err != nil {
		serializer.Fail(ctx, "上传失败")
		return
	}

	serializer.SuccessData(ctx, "", upload)
}

// @Tags 附件
// @Summary 删除以过期的附件
// @Security ApiKeyAuth
// @function: DeleteExpireFile
// @Accept  json
// @Produce  json
// @Success 200 {string} string "{"success":true,"data":{},"msg":""}"
// @Router /file/delete/expire [delete]
func (c *FileController) DeleteExpireFile(ctx *gin.Context) {
	err := business.ServiceImpl().FileService.FindExpireFiles()
	if err != nil {
		serializer.Fail(ctx, serializer.PublicUpdateErr)
		return
	}

	serializer.Success(ctx, "")
}

//分片上传本地视频
func (c *FileController) Chunk(ctx *gin.Context) {
	form := request.FileChunkRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	file, header, err := ctx.Request.FormFile("file")

	defer file.Close()

	if err != nil {
		serializer.Fail(ctx, "获取文件失败")
		return
	}

	validFile, err := uploader.ValidUploadRequest(file, header)
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	chunk, err := business.ServiceImpl().FileService.UploadChunk(&uploader.ShardFileRequest{
		ChunkNumber:     form.ChunkNumber,
		ChunkSize:       form.ChunkSize,
		ActualChunkSize: form.ActualChunkSize,
		TotalSize:       form.TotalSize,
		Filename:        form.Filename,
		TotalChunks:     form.TotalChunks,
		Hash:            form.Hash,
		FileByte:        validFile.FileStream,
		UniqAt:          form.UniqAt,
	})

	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	variables.GormDB().Model(&model.File{}).Where("md5 = ?")

	//business.ServiceImpl().FileService.Create(&model.File{
	//	RealName:     form.Filename,
	//	Name:         form.Filename,
	//	LocationType: chunk.LocationType,
	//	UserId:       userId,
	//	AttDir:       chunk.AttDir,
	//	AttSize:      chunk.AttSize,
	//	AttType:      chunk.AttType,
	//	ContentType:  chunk.ContentType,
	//	Hash:         form.Hash,
	//	Bucket:       chunk.Bucket,
	//	Key:          "",
	//	Status:       constant.FILE_NORMAL,
	//	ChunkType:    0,
	//})

	serializer.SuccessData(ctx, "", chunk)
}
